package com.kaiwukj.android.ufamily.mvp.model;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.GsonUtils;
import com.kaiwukj.android.mcas.di.scope.FragmentScope;
import com.kaiwukj.android.mcas.integration.IRepositoryManager;
import com.kaiwukj.android.mcas.mvp.BaseModel;
import com.kaiwukj.android.ufamily.mvp.http.api.service.ActiveService;
import com.kaiwukj.android.ufamily.mvp.http.api.service.CommunityService;
import com.kaiwukj.android.ufamily.mvp.http.api.service.DynamicService;
import com.kaiwukj.android.ufamily.mvp.http.api.service.ServiceService;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.ActiveParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.CommentParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.DynamicListParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ActiveResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.CommunityInfoResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.CommunityNewResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.CommunityNoticeResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DynamicResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.FriendRecoResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.home.home.contract.HomeV20Contract;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;

@FragmentScope
public class HomeV20Model extends BaseModel implements HomeV20Contract.Model {

    @Inject
    public HomeV20Model(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }


    @NotNull
    @Override
    public Observable<ListResp<ActiveResult>> fetchActive(ActiveParams params) {
        return mRepositoryManager.obtainRetrofitService(ActiveService.class)
                .fetchActiveList(params.getPage(),
                        params.getPageSize(),
                        params.getPubType(),
                        params.getStatus(),
                        params.getType())
                .compose(RxUtils.handleResult());
    }

    @NotNull
    @Override
    public Observable<ListResp<DynamicResult>> fetchDynamic(@NotNull DynamicListParams params) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .fetchDynamic(params.getStartId(),
                        params.getPageSize(),
                        params.getTopicId(),
                        params.getTopicTitle(),
                        params.getIsHot(),
                        params.getType(),
                        params.getUserId())
                .compose(RxUtils.handleResult());
    }

    @NotNull
    @Override
    public Observable<ListResp<DynamicResult>> fetchAttentionDynamic(int startId, int pageSize, int type) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .fetchAttentionDynamic(startId, pageSize, type)
                .compose(RxUtils.handleResult());
    }

    @NotNull
    @Override
    public Observable<List<ActiveResult>> fetchHotActive(int type) {
        return mRepositoryManager.obtainRetrofitService(ActiveService.class)
                .fetchActiveHot(5, type)
                .compose(RxUtils.handleResult());
    }

    @NotNull
    @Override
    public Observable<ActiveResult> getActiveDetail(int id) {
        return mRepositoryManager.obtainRetrofitService(ActiveService.class)
                .getActiveById(id)
                .compose(RxUtils.handleResult());
    }

    @NotNull
    @Override
    public Observable<Integer> join(int id) {
        Map<String, Object> params = new HashMap<>();
        params.put("activityId", id);
        return mRepositoryManager.obtainRetrofitService(ActiveService.class)
                .joinActive(getRequestBody(JSON.toJSONString(params)))
                .compose(RxUtils.handleCode());
    }

    @NotNull
    @Override
    public Observable<Integer> unJoin(int id) {
        return mRepositoryManager.obtainRetrofitService(ActiveService.class)
                .cancelJoin(id)
                .compose(RxUtils.handleCode());
    }

    @NotNull
    @Override
    public Observable<List<FriendRecoResult>> fetchRecoAttention(int type) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .fetchRecoAttention(type)
                .compose(RxUtils.handleResult());
    }

    @NotNull
    @Override
    public Observable<Integer> postLike(int id) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .praise(id)
                .compose(RxUtils.handleCode());
    }

    @NotNull
    @Override
    public Observable<Integer> postNoLike(int id) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .cancelPraise(id)
                .compose(RxUtils.handleCode());
    }

    @NotNull
    @Override
    public Observable<Integer> attention(int id) {
        Map<String, Object> params = new HashMap<>();
        params.put("beConcernId", id);
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .attention(getRequestBody(GsonUtils.toJson(params)))
                .compose(RxUtils.handleCode());
    }

    @NotNull
    @Override
    public Observable<ListResp<ActiveResult>> fetchMyActive(int page, int pageSize) {
        return mRepositoryManager.obtainRetrofitService(ActiveService.class)
                .fetchMyActive(page, pageSize, null)
                .compose(RxUtils.handleResult());
    }

    @NotNull
    @Override
    public Observable<Integer> comment(@NotNull CommentParams params) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .comment(getRequestBody(JSON.toJSONString(params)))
                .compose(RxUtils.handleResult());
    }

    @NotNull
    @Override
    public Observable<ListResp<CommunityNoticeResult>> fetchNotice() {
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .fetchCommunityNotice(1, 1)
                .compose(RxUtils.handleResult());
    }

    @NotNull
    @Override
    public Observable<Integer> shield(int uId, boolean block, boolean user) {
        Map<String, Object> params = new HashMap<>();
        params.put("resourceId", uId);
        params.put("statusFlag", block ? 1 : 0);
        params.put("type", user ? 2 : 1);
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .shield(getRequestBody(GsonUtils.toJson(params)))
                .compose(RxUtils.handleCode());
    }

    @NotNull
    @Override
    public Observable<Integer> delDynamic(int dynamicId) {
        return mRepositoryManager.obtainRetrofitService(DynamicService.class)
                .deleteDynamic(dynamicId)
                .compose(RxUtils.handleCode());
    }


    @NotNull
    @Override
    public Observable<CommunityInfoResult> getCommunityInfo(int communityId) {
        return mRepositoryManager.obtainRetrofitService(CommunityService.class)
                .getCommunityInfo(communityId)
                .compose(RxUtils.handleResult());
    }

    @NotNull
    @Override
    public Observable<ListResp<CommunityNewResult>> fetchCommunityNews(int communityId) {
        return mRepositoryManager.obtainRetrofitService(ServiceService.class)
                .fetchCommunityNewList(communityId)
                .compose(RxUtils.handleResult());
    }
}
